import 'server-only' //代表仅服务端可用
import Joi from 'joi';
import resFn from '@/app/api/util/res'
import { NextRequest } from 'next/server';
import { logError, logSpecial } from '@/app/api/util/io/log';
import { getBody, judge } from '@/app/api/util/res/handle';
import JWT from '../../util/jwt';
import { generateReq,generateRes } from '@/types/api/generate';
import { RejectData,ResCode } from '../../util/res/code';
import { stringJoi } from '../../util/Joi';
import axios from 'axios';
export const dynamic = 'force-dynamic';

/** 生成接口 请求路径api/generate*/
export async function POST(request: NextRequest) {
  try {
    // const { user } = await JWT.getTokenAndData(request)
    const { kit,img_url} = await judge(await getBody<generateReq>(request), {kit:stringJoi(),img_url:stringJoi()})
    const res = await axios.post('http://10.12.38.9:8004/venus2_kits', { user:"JZ8740",kit,img_url})
    logSpecial("生成结果",res.data)
    if(res.data.code!==0)throw new RejectData(ResCode.FAIL_REQUEST, res.data.msg as any)

    return resFn.success<generateRes>(res.data.data);
  } catch (error: any) {
    logError('功能体验-生成接口', error)
    return resFn.errorWithReject(error);
  }
}